<style>
    a{color:#2e52b9;}
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <form method="post" action="<?= $this->projectSet["url"]["list"] ?>">
            <input type="hidden" name="language_id" value="<?= !empty($language_id) ? $language_id : 1; ?>">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="order_no" value="<?= $order_no ?>" placeholder="订单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="order_date_range" id="order_date_range" value="<?= $order_date_range ?>" placeholder="订单日期" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="kehu_custom_id" lay-search>
                                <option value="">客户</option>
                                <?php foreach ($kehu as $k => $v) { ?>
                                    <option value="<?= $v["id"] ?>"<?= isset($kehu_custom_id) && $kehu_custom_id == $v["id"] ? " selected" : "" ?>><?= $v["cmp_bf"] ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="car_id" lay-search>
                                <option value="">车牌号</option>
                                <?php foreach ($car as $k => $v) { ?>
                                    <option value="<?= $v["id"] ?>"<?= isset($car_id) && $car_id == $v["id"] ? " selected" : "" ?>><?= $v["car_number"] ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="hexiao_status">
                                <option value="">核销状态</option>
                                <?php foreach ($hexiaoStatus as $k => $v) { ?>
                                    <option value="<?= $k ?>"<?= isset($hexiao_status) && $hexiao_status === strval($k) ? " selected" : "" ?>><?= $v["name"] ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-list customize" lay-submit lay-filter="LAY-app-contlist-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="layui-card">
        <div class="layui-card-header">
            <span><?= $navName ?></span>
            <!--
            <span style="float:right;">
                <a href="javascript:;" onclick="return add()" class="layui-btn layui-btn-sm customize">新增订单</a>
            </span>
            -->
        </div>
        <div class="layui-card-body">
            <table class="layui-hide" id="table-data" lay-filter="table-data" style="overflow-x:scroll;"></table>
            <!--
            <tool id="table-toolbar" style="display:none">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="batchPay">
                        付款
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="batchHexiao">
                        核销
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="batchCancelHexiao">
                        取消核销
                    </button>
                </div>
            </tool>
            -->
        </div>
    </div>
</div>
<script>
    var viewUrl = "<?= $this->projectSet["url"]["view"] ?>";
    var batchPayUrl = "<?= $this->projectSet["url"]["batchPay"] ?>";
    var hexiaoUrl = "<?= $this->projectSet["url"]["hexiao"] ?>";
    var cancelHexiaoUrl = "<?= $this->projectSet["url"]["cancelHexiao"] ?>";
    var doSelectIdsUrl = "<?= $this->projectSet["url"]["doSelectIds"] ?>";
    var so_num = "<?= $listNum ?>";
    var pageStr = "<?= $pageStr ?>";
</script>
<script>
    layui.use(['laypage', 'layer', 'form', 'table', 'laydate'], function () {
        var laypage = layui.laypage
                , table = layui.table
                , frm = layui.form
                , layer = layui.layer
                , laydate = layui.laydate;

        laydate.render({
            elem: '#order_date_range' //指定元素
                    //type: 'date'
            , range: true
        });

        //var tableObj = table.render({});
        var cookiePageArray = [];
        //存储已选择数据集，用普通变量存储也行，就是一个缓存变量
        layui.data('checked', null);
        table.render({
            elem: '#table-data'
            , url: '<?= $base_url ?>'
                    //, toolbar: '#table-toolbar'
//            , defaultToolbar: ['filter', {
//                    title: '导出所有搜索结果' //标题
//                    , layEvent: 'export_all' //事件名，用于 toolbar 事件中使用
//                    , icon: 'layui-icon-export' //图标类名
//                }]
            , defaultToolbar: ['filter']
                    //, title: '<?= $navName ?>'
            , size: 'sm'
            , cellMinWidth: 80
            , height: 500
            , cols: [[
                    //{type: 'checkbox', fixed: 'left', LAY_CHECKED: false}
                    //, {field: 'id', title: 'ID号', width: 60, fixed: 'left'}
                    {field: 'order_no', title: '订单号', fixed: 'left', width: 140
                        , templet: function (d) {
                            return "<a href='javascript:;' onclick='return batchPay(" + d.id + ")'>" + d.order_no + "</a>";
                        }
                    }
                    , {field: 'hexiao_status_tag', title: '核销状态'}
                    , {field: 'status_name', title: '订单状态'}
                    , {field: 'order_date_alias', title: '接单日期', width: 100}
                    , {field: 'ct_cmp_bf', title: '客户'}
                    , {field: 'org_tag', title: '提货点'}
                    , {field: 'dest_tag', title: '卸货点'}
                    , {field: 'diaodu_truename', title: '调度'}
                    , {field: 'c_car_number', title: '车牌号',width:100}
                    , {field: 'total_yingfu', title: '应付'
                        , templet: function (d) {
                            if (d.total_yingfu) {
                                return "<a href='javascript:;' onclick='return batchPay(" + d.id + ")'>" + d.total_yingfu + "</a>";
                            } else {
                                return "<a href='javascript:;' onclick='return batchPay(" + d.id + ")'>0</a>";
                            }
                        }
                    }
                    , {field: 'total_yifu', title: '已付'}
                    , {field: 'total_mileage', title: '公里数'}
                    , {field: 'youhao', title: '油耗'}
                ]
            ]
            , done: function (res, curr, count) {
                //.假设你的表格指定的 id="sale-data"，找到框架渲染的表格
                var tbl = $('#table-data').next('.layui-table-view');
                //记下当前页数据，Ajax 请求的数据集，对应你后端返回的数据字段
                cookiePageArray = res.data;
                //记录当前也选中的chechbox个数(判断全选使用的)
                var len = 0;
                //遍历当前页数据，对比已选中项中的 id
                for (var index = 0; index < cookiePageArray.length; index++) {
                    //console.log(cookiePageArray);
                    if (layui.data('checked', cookiePageArray[index]['id'])) {
                        //逐个判断是否选中，如果已经选中就改变选中模式
                        var i = cookiePageArray[index]['LAY_TABLE_INDEX'];
                        //console.log(i);
                        var checkbox = $('.layui-table tr[data-index=' + i + '] input[type="checkbox"]');
                        checkbox.prop('checked', true);
                        checkbox.next().addClass('layui-form-checked');
                        //checkbox.parent().parent().parent().addClass('layui-table-click');
                        //res.data[i].LAY_CHECKED = true;
                        //为了设置全选的
                        len++;
                    }
                }

                //console.log(this.limit + "=====" + len);
                //设置全选checkbox的选中状态，只有改变LAY_CHECKED的值， table.checkStatus才能抓取到选中的状态
                if (len === this.limit) {
                    var checkboxAll = $('.layui-table-header th[data-field=0] input[type="checkbox"]');
                    checkboxAll.prop('checked', true);
                    checkboxAll.next().addClass('layui-form-checked');
                    //checkboxAll.parent().parent().parent().addClass('layui-table-click');
                }
                //暂时只能这样渲染表单
                frm.render('checkbox');
            }
            , page: {//支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'limit'] //自定义分页布局
                        //,curr: 5 //设定初始在第 5 页
                , groups: 5 //只显示 1 个连续页码
                        //, first: false //不显示首页
                        //, last: false //不显示尾页
                , limit: <?= $pageCount ?>
                , limits: [15, 30, 50, 100, 200]
                , prev: '上一页'
                , next: '下一页'
            }
        });
        //头工具栏事件
        table.on('toolbar(table-data)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var selectData = layui.data('checked', "select_data") ? layui.data('checked').select_data : [];
            switch (obj.event) {
                case 'batchPay':
                    var length = selectData.length;
                    if (length <= 0) {
                        layer.msg('请选择订单');
                        return false;
                    }
                    batchPay();
                    break;
                case 'batchHexiao':
                    var length = selectData.length;
                    if (length <= 0) {
                        layer.msg('请选择订单');
                        return false;
                    }
                    batchHexiao();
                    break;
                case 'batchCancelHexiao':
                    var length = selectData.length;
                    if (length <= 0) {
                        layer.msg('请选择订单');
                        return false;
                    }
                    batchCancelHexiao();
                    break;
            }
        });
        table.on('checkbox(table-data)', function (obj) {
            //console.log(obj.type); //如果触发的是全选，则为：all，如果触发的是单选，则为：one
            //全选或单选数据集不一样
            var data = 'one' === obj.type ? [obj.data] : cookiePageArray;
            var selectData = layui.data('checked', "select_data") ? layui.data('checked').select_data : [];
            //遍历数据
            $.each(data, function (index, item) {
                //假设你数据中 id 是唯一关键字
                if (obj.checked) {
                    //alert(obj.tr);
                    //console.log(obj.tr);
                    //.增加已选中项
                    layui.data('checked', {
                        key: item.id, value: item
                    });
                    if (selectData.indexOf(item.id) > -1) {//如果原来的键值存在,则不操作

                    } else {
                        selectData.push(item.id);
                    }
                    //obj.tr.addClass('layui-table-click');
                } else {
                    //alert(0);
                    //console.log(0);
                    //.删除
                    layui.data('checked', {
                        key: item.id, remove: true
                    });
                    selectData.splice(selectData.indexOf(item.id), 1);
                    //obj.tr.removeClass('layui-table-click');
                }
                layui.data('checked', {
                    key: "select_data", value: selectData
                });
            });
            var ids = layui.data('checked', "select_data") ? layui.data('checked').select_data : [];
            $.ajax({
                type: "POST",
                dataType: "json",
                url: doSelectIdsUrl,
                data: {
                    ids: ids
                },
                success: function (data) {
                }
            });
            //console.log(layui.data('checked'));
        });
    });
</script>
<script src="<?= PUBLIC_URL . "/" . $this->projectSet["file"]["js"] . "/list.js?" . rand() ?>"></script>

